<?php

use Biz\service\trade\Order as OrderService;
use Doraemon\tools\ActionLog;

class CreateAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $skuToken = $this->getRequest()->getPost('sku_token', '');
        $acceptId = $this->getRequest()->getPost('accept_id', '');
        $messageInfo = $this->getRequest()->getPost('message_info', '');
        $integral = $this->getRequest()->getPost('integral', 0);
        $messageInfo = $messageInfo ? json_decode($messageInfo, true) : '';
        $couponId = $this->getRequest()->getPost('coupon_id', null);
        $couponVId = $this->getRequest()->getPost('coupon_vid', null);
        $sCouponInfoStr = $this->getRequest()->getPost('s_coupon_info', null);
        $mealId = $this->getRequest()->getPost('meal_id', 0);
        $sCouponInfoArr = $sCouponInfoStr ? json_decode($sCouponInfoStr, true) : null;

        $storeId = $this->getRequest()->getPost('store_id', 0);
        $isSelfMention = $this->getRequest()->getPost('self_mention', 0);

        $activityId = $this->getRequest()->getPost('activity_id', 0);
        $activityType = $this->getRequest()->getPost('activity_type', 0);
        $teamJoinId = $this->getRequest()->getPost('team_join_id', 0);
        $packetAmount = $this->getRequest()->getPost('packet_amount', 0);
        $packetAmount = $packetAmount ? $packetAmount : 0;
        $sCouponInfo = null;
        if ($sCouponInfoArr) {
            foreach ($sCouponInfoArr as $row) {
                $sCouponInfo[$row['shop_id']] = $row['coupon_info'];
            }
        }
        if (!$acceptId) {
            throw new Exception('请填写收货地址', -1);
        }
        if (!$skuToken) {
            throw new Exception('参数错误');
        }
        $list = OrderService::create($this->uid, $skuToken, $acceptId, $messageInfo, $integral, $packetAmount, $couponId, $couponVId, $sCouponInfo, $this->appType, $mealId, $storeId, $isSelfMention, $activityId, $activityType, $teamJoinId);
        $orderParentNo = ($list && isset($list['order_no'])) ? $list['order_no'] : '';
        ActionLog::log($this->uid, ['order_parent_no' => $orderParentNo], ActionLog::BIZ_ORDER_CREATE);
        $this->setSuccess($list);
    }
}
